package com.wy.panda.controller;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wy.panda.common.model.R;
import com.wy.panda.common.page.PageResult;
import com.wy.panda.controller.po.BasePO.CreateGroup;
import com.wy.panda.controller.po.BasePO.UpdateGroup;
import com.wy.panda.controller.po.SysDictPO;
import com.wy.panda.controller.query.BaseQuery.QueryGroup;
import com.wy.panda.controller.query.SysDictQuery;
import com.wy.panda.controller.vo.SysDictVO;
import com.wy.panda.dao.criteria.SysDictCriteria;
import com.wy.panda.entity.SysDictEntity;
import com.wy.panda.service.SysDictService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

/**
 * 数据字典
 *
 * @author wangyuan
 * @since 2021-03-27 13:27:03
 */
@Api(value = "数据字典", tags = "数据字典")
@Validated
@AllArgsConstructor
@RestController
@RequestMapping("/sys/dict")
public class SysDictController extends BaseController {

    private final SysDictService sysDictService;
    
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/list")
    public R<PageResult<SysDictVO>> list(@Validated(QueryGroup.class) @ModelAttribute SysDictQuery query) {
        PageResult<SysDictEntity> result = sysDictService.listPage(query.convert(SysDictCriteria.class));
        return R.ok(result.convert(SysDictEntity.class, SysDictVO.class));
    }
    
    @ApiOperation(value = "详情")
    @GetMapping(value = "/info/{id}")
    public R<SysDictVO> info(@ApiParam("数据字典id") @NotNull(message = "参数id不能为空") @PathVariable("id") Long id) {
        SysDictEntity entity = sysDictService.getById(id);
        if(null == entity) {
            return R.error("查询不到记录");
        }
        return R.ok(entity.convert(SysDictVO.class));
    }
    
    @ApiOperation(value = "保存")
    @PostMapping(value = "/save")
    public R<?> save(@Validated(CreateGroup.class) @ModelAttribute SysDictPO po) {
        SysDictEntity entity = po.convert(SysDictEntity.class);
        sysDictService.save(entity);
        return R.ok();
    }
    
    @ApiOperation(value = "更新")
    @PostMapping(value = "/update")
    public R<?> update(@Validated(UpdateGroup.class) @ModelAttribute SysDictPO po) {
        SysDictEntity oldEntity = sysDictService.getById(po.getId());
        if(null == oldEntity) {
            return R.error("更新失败，记录不存在。");
        }
        
        mapperFacade.map(po, oldEntity);
        
        sysDictService.updateById(oldEntity);
        
        return R.ok();
    }
    
    @ApiOperation(value = "（批量）删除")
    @PostMapping(value = "/delete/{ids}")
    public R<?> deleteBatch(@ApiParam("id，多个使用,分隔") @NotNull(message = "参数ids不能为空") @PathVariable List<Long> ids) {
        sysDictService.removeByIds(ids);
        return R.ok();
    }
    
}